﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMS.DB
{
    class ConfigurationMng : DBController
    {
        public List<Configuration> GetAllItems(string type)
        {
            var listItems = new List<Configuration>();
            try
            {
                var query = from item in DataEntities.Configurations
                            where item.Type.Equals(type)
                            orderby item.Name
                            select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }

        public bool AddItem(string type, string name)
        {
            try
            {
                var item = new Configuration() { Type = type,
                Name = name };
                var currentList = GetAllItems(type);
                foreach (var i in currentList)
                {
                    if (i.Name.ToLower() == name.ToLower())
                    {
                        MessageBox.Show("Tên này đã tồn tại. Xin vui lòng nhập lại!");
                        return false;
                    }
                }
                DataEntities.AddToConfigurations(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool AddStyleItem(string type, string name, string imageName)
        {
            try
            {
                var item = new Configuration() { Type = type,
                Name = name,
                Description = imageName };
                var currentList = GetAllItems(type);
                foreach (var i in currentList)
                {
                    if (i.Name.ToLower() == name.ToLower())
                    {
                        MessageBox.Show("Tên này đã tồn tại. Xin vui lòng nhập lại!");
                        return false;
                    }
                }
                DataEntities.AddToConfigurations(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public Configuration GetItemByElements(int id)
        {
            var config = new Configuration();
            try
            {
                IQueryable<Configuration> query = from item in DataEntities.Configurations
                                                  where item.ID == id
                                                  select item;

                config = query.FirstOrDefault();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return config;
        }
        public bool DeleteItem(int id)
        {
            try
            {
                var item = new Configuration();
                item = GetItemByElements(id);
                DataEntities.DeleteObject(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool UpdateItem(int id, string name)
        {
            try
            {
                Configuration item;
                item = GetItemByElements(id);
                if (item == null)
                {
                    MessageBox.Show(Constants.Msg.NotItemExist);
                    return false;
                }
                item.Name = name;
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool UpdateStyleItem(int id, string name, string imageName)
        {
            try
            {
                Configuration item;
                item = GetItemByElements(id);
                if (item == null)
                {
                    MessageBox.Show(Constants.Msg.NotItemExist);
                    return false;
                }
                item.Name = name;
                if (!string.IsNullOrEmpty(imageName))
                    item.Description = imageName;
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
    }
}
